package com.common.supply.service.impl.extdata;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.supply.Utils.Func;
import com.common.supply.common.GlobalConstant;
import com.common.supply.common.RspEnmu;
import com.common.supply.config.dynamicdb.DynamicMultipleDataSource;
import com.common.supply.config.exception.BusinessException;
import com.common.supply.domain.entity.business.BankCode;
import com.common.supply.domain.entity.extdata.ExtIdentityResult;
import com.common.supply.domain.dto.IdentifyDto;
import com.common.supply.mapper.extdata.ExtIdentityResultMapper;
import com.common.supply.service.IBankCodeService;
import com.common.supply.service.IExtIdentityResultService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 身份验证数据Service业务层处理
 *
 * @author african-loan
 * @date 2021-12-30
 */
@Service
@AllArgsConstructor
public class ExtIdentityResultServiceImpl extends ServiceImpl<ExtIdentityResultMapper, ExtIdentityResult> implements IExtIdentityResultService {

    @Autowired
    private DynamicMultipleDataSource dataSource;

    @Autowired
    private IBankCodeService bankCodeService;


    @Autowired
    private OnepipeService onepipeService;

    private ThreadLocal<String> kycExqApiThreadLocal = new ThreadLocal<>();

    @Override
    public IdentifyDto queryIdentityInfo(IdentifyDto identifyDto) {

        //查询本地数据
        ExtIdentityResult localExtIdentity = getLocalExtIdentity(identifyDto.getBvn(), identifyDto.getAccount());
        if (localExtIdentity != null) {
            kycExqApiThreadLocal.set(localExtIdentity.getApiName());
            identifyDto.setExtIdentityResult(localExtIdentity);
            return identifyDto;
        }
        //返回使用的是那个接口的数据
        if (Func.isNotEmpty(identifyDto.getAccount())) {

        }

        return null;
    }

    /**
     * 获取本地数据
     * @param bvn
     * @param account
     * @return
     */
    private ExtIdentityResult getLocalExtIdentity(String bvn, String account) {
        QueryWrapper<ExtIdentityResult> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper.eq("bvn", bvn).eq("state", GlobalConstant.BIZ_STATUS_1));
        if (Func.isNotEmpty(account)) {
            queryWrapper.or(wrapper -> wrapper.eq("account_number", account).in("api_name", "Lookup-Account-Mid", "NUBAN-Identity-Verification", "NUBAN-Identity-Verification-back").eq("state", GlobalConstant.BIZ_STATUS_1));
        }
        queryWrapper.last("order by id desc limit 1");
        List<ExtIdentityResult> list = this.list(queryWrapper);
        return list.size() > 0 ? list.get(0) : null;
    }



}
